﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;
using System.Threading;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DB_CheckCharacterName (smp_insert_character)
    /// </summary>
    public class DB_CreateCharacter : IDBProc
    {
        private string m_Name;
        private int m_nOwnerID;
        private int m_nRace;
        private int m_nSex;
        private uint m_nSkinColor;
        private int m_nAccountID;
        private int m_wear_item;
        private string m_AccountName;
        private int[] m_nModelId = new int[5];
        private int m_hair_dye;
        private int m_hair_cut;
        private int m_unk1;
        private int m_face;

        private TCPConnection m_client;

        private enum DBFields : int
        {
            Count = 0,
        }
        public DB_CreateCharacter(TCPConnection client, string szName, string szAccountName, int nAccountId, int race, int sex, uint skin_color, int model_00, int model_01, int model_02, int model_03, int model_04, int hair_dye, int hair_cut, int unk1, int face, int _wear_item)
        {
            m_client = client;
            m_Name = szName;

            m_nOwnerID = 0;
            m_nRace = race;
            m_nSex = sex;
            m_nSkinColor = skin_color;
            m_nAccountID = nAccountId;
            m_wear_item = _wear_item;
            m_Name = szName;
            m_AccountName = szAccountName;
            m_nModelId[0] = model_00;
            m_nModelId[1] = model_01;
            m_nModelId[2] = model_02;
            m_nModelId[3] = model_03;
            m_nModelId[4] = model_04;
            m_hair_dye = hair_dye;
            m_hair_cut = hair_cut;
            m_unk1 = unk1;
            m_face = face;

        }

        BaseObject m_CBObject;

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = GameDBManager.Instance.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "smp_insert_character";
            cmd.CommandType = CommandType.StoredProcedure;

            int nSlot = 0;
            int nDefaultBagCode = 490001;
            int nDefaultArmorCode = 220100;
            if (m_wear_item == 602)
                nDefaultArmorCode = 220109;

            int nDefaultWeaponCode = 106100;
            if (m_nRace == 3)
            {
                nDefaultArmorCode = 240100;
                if (m_wear_item == 602)
                    nDefaultArmorCode = 240109;
                nDefaultWeaponCode = 112100;
            }
            else
            {
                if (m_nRace == 5)
                {
                    nDefaultArmorCode = 230100;
                    if (m_wear_item == 602)
                        nDefaultArmorCode = 230109;
                    nDefaultWeaponCode = 103100;
                }
            }

            DbParameter return_value = GameDBManager.Instance.CreateParameter("RETURN_VALUE");
            return_value.DbType = DbType.Int32;
            return_value.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(return_value);

            DbParameter out_sid = GameDBManager.Instance.CreateParameter("OUT_SID");
            out_sid.DbType = DbType.Int32;
            out_sid.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(out_sid);

            int nX = 19500;
            int nY = 51000;
            int nZ = 0;
            int nLayer = 0;
            int nLevel = 0;
            int nMRLevel = 0;
            int nHP = 0;
            int nMP = 0;
            int nJL = 0;
            int nJP = 0;
            int nCHA = 0;
            long nDefaultWeaponSid = Interlocked.Increment(ref Player.s_nItemIndex);
            long nDefaultArmorSid = Interlocked.Increment(ref Player.s_nItemIndex);
            long nDefaultBagSid = Interlocked.Increment(ref Player.s_nItemIndex);


            DbParameter in_name = GameDBManager.Instance.CreateInParameter(cmd, "IN_NAME", DbType.String, m_Name);
            DbParameter in_account = GameDBManager.Instance.CreateInParameter(cmd, "IN_ACCOUNT", DbType.String, m_AccountName);
            DbParameter in_account_id = GameDBManager.Instance.CreateInParameter(cmd, "IN_ACCOUNT_ID", DbType.Int32, m_nAccountID);
            DbParameter in_slot = GameDBManager.Instance.CreateInParameter(cmd, "IN_SLOT", DbType.Int32, nSlot);
            DbParameter in_x = GameDBManager.Instance.CreateInParameter(cmd, "IN_X", DbType.Int32, nX);
            DbParameter in_y = GameDBManager.Instance.CreateInParameter(cmd, "IN_Y", DbType.Int32, nY);
            DbParameter in_z = GameDBManager.Instance.CreateInParameter(cmd, "IN_Z", DbType.Int32, nZ);
            DbParameter in_layer = GameDBManager.Instance.CreateInParameter(cmd, "IN_LAYER", DbType.Int32, nLayer);
            DbParameter in_race = GameDBManager.Instance.CreateInParameter(cmd, "IN_RACE", DbType.Int32, m_nRace);
            DbParameter in_sex = GameDBManager.Instance.CreateInParameter(cmd, "IN_SEX", DbType.Int32, m_nSex);
            DbParameter in_lv = GameDBManager.Instance.CreateInParameter(cmd, "IN_LV", DbType.Int32, nLevel);
            DbParameter in_mrlv = GameDBManager.Instance.CreateInParameter(cmd, "in_mrlv", DbType.Int32, nMRLevel);
            DbParameter in_hp = GameDBManager.Instance.CreateInParameter(cmd, "IN_HP", DbType.Int32, nHP);
            DbParameter in_mp = GameDBManager.Instance.CreateInParameter(cmd, "in_mp", DbType.Int32, nMP);
            DbParameter in_jlv = GameDBManager.Instance.CreateInParameter(cmd, "in_jlv", DbType.Int32, nJL);
            DbParameter in_jp = GameDBManager.Instance.CreateInParameter(cmd, "IN_JP", DbType.Int32, nJP);
            DbParameter in_cha = GameDBManager.Instance.CreateInParameter(cmd, "IN_CHA", DbType.Int32, nCHA);
            DbParameter in_skin_color = GameDBManager.Instance.CreateInParameter(cmd, "IN_SKIN_COLOR", DbType.Int32, m_nSkinColor);
            DbParameter in_model_00 = GameDBManager.Instance.CreateInParameter(cmd, "IN_MODEL_00", DbType.Int32, m_nModelId[0]);
            DbParameter in_model_01 = GameDBManager.Instance.CreateInParameter(cmd, "IN_MODEL_01", DbType.Int32, m_nModelId[1]);
            DbParameter in_model_02 = GameDBManager.Instance.CreateInParameter(cmd, "IN_MODEL_02", DbType.Int32, m_nModelId[2]);
            DbParameter in_model_03 = GameDBManager.Instance.CreateInParameter(cmd, "IN_MODEL_03", DbType.Int32, m_nModelId[3]);
            DbParameter in_model_04 = GameDBManager.Instance.CreateInParameter(cmd, "IN_MODEL_04", DbType.Int32, m_nModelId[4]);

            DbParameter in_hair_color_index = GameDBManager.Instance.CreateInParameter(cmd, "IN_HAIR_COLOR_INDEX", DbType.Int32, m_hair_dye);
            DbParameter in_face = GameDBManager.Instance.CreateInParameter(cmd, "IN_TEXTURE_ID", DbType.Int32, m_face);
            DbParameter in_default_weapon_sid = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_WEAPON_SID", DbType.Int64, nDefaultWeaponSid);
            DbParameter in_default_weapon_code = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_WEAPON_CODE", DbType.Int32, nDefaultWeaponCode);
            DbParameter in_default_armor_sid = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_ARMOR_SID", DbType.Int32, nDefaultArmorSid);
            DbParameter in_default_armor_code = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_ARMOR_CODE", DbType.Int32, nDefaultArmorCode);
            DbParameter in_default_bag_sid = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_BAG_SID", DbType.Int64, nDefaultBagSid);
            DbParameter in_default_bag_code = GameDBManager.Instance.CreateInParameter(cmd, "IN_DEFAULT_BAG_CODE", DbType.Int32, nDefaultBagCode);

            
/*
                DbParameter in_haircut = GameDBManager.Instance.CreateParameter("IN_HAIRCUT");
                in_haircut.DbType = DbType.Int32;
                in_haircut.Direction = ParameterDirection.Input;
                in_haircut.Value = m_hair_cut;
                cmd.Parameters.Add(in_haircut);

                DbParameter in_unk1 = GameDBManager.Instance.CreateParameter("IN_UNK1");
                in_unk1.DbType = DbType.Int32;
                in_unk1.Direction = ParameterDirection.Input;
                in_unk1.Value = m_unk1;
                cmd.Parameters.Add(in_unk1);
*/
            


            conn.Open();
            cmd.Prepare();

            try
            {
                //Fixme: not cmd.ExecuteNonQuery(); ?
                cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DB_CreateCharacter Exception: {0}", ex);
            }




            if ((int)return_value.Value == -1)
            {
                Messages.SendResult(m_client, 2002, 9, 0);
            }
            else
            {
                Messages.SendResult(m_client, 2002, 0, 0);

                //This method is not implemented
                ServerLog.Log(2001, 0,0,0,0,0,0,0,0,0,0,0, m_AccountName, m_Name,"","");
            }

            return true;
        }
    }
}
